package com.wing.cabinet.utils;

import com.sun.mail.util.MailSSLSocketFactory;
import com.wing.cabinet.dao.MailInfo;
import com.wing.common.exception.BusinessException;
import lombok.AllArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import net.bytebuddy.asm.Advice;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.mail.javamail.JavaMailSenderImpl;
import org.springframework.mail.javamail.MimeMessageHelper;
import org.springframework.stereotype.Component;

import javax.mail.*;
import javax.mail.internet.MimeMessage;
import java.security.GeneralSecurityException;
import java.util.HashMap;
import java.util.Properties;

@Slf4j
@Component
@AllArgsConstructor
public class MailSenderConfig {


    public void sendMail(String userName, String password, String host, String ssl, Integer port, String toEmail, String subject, String text) throws MessagingException, GeneralSecurityException {
        try {

            final JavaMailSenderImpl javaMailSender = new JavaMailSenderImpl();
            javaMailSender.setUsername(userName);
            javaMailSender.setPassword(password);
            javaMailSender.setHost(host);
//            javaMailSender.setPort(port);
//            javaMailSender.setProtocol("smtp");
//            javaMailSender.setDefaultEncoding(defaultEncoding);
            final Properties mailProperties = new Properties();

            MailSSLSocketFactory sf = new MailSSLSocketFactory();
            sf.setTrustAllHosts(true);

            mailProperties.put("mail.transport.protocol", "smtp");
            mailProperties.put("mail.smtp.ssl.socketFactory", sf);
            //是否启用 SSL 加密功能，保障邮件内容的安全。
            if (Integer.valueOf(ssl)==0){
                mailProperties.put("mail.smtp.ssl.enable", "false");
            }
            if (Integer.valueOf(ssl)==1){
                mailProperties.put("mail.smtp.ssl.enable", "true");
                mailProperties.put("mail.smtp.ssl.protocols", "TLSv1.2");
            }
            javaMailSender.setJavaMailProperties(mailProperties);
            final MimeMessage mimeMessage = getMimeMessage(toEmail, subject, text, javaMailSender,userName);
            javaMailSender.send(mimeMessage);
        } catch (MessagingException | GeneralSecurityException e) {
            throw new RuntimeException("Failed to send email", e);
        }
    }


    private MimeMessage getMimeMessage(String toEmail, String subject, String text, JavaMailSenderImpl javaMailSender, String senderName) throws javax.mail.MessagingException {
        MimeMessage mimeMessage = javaMailSender.createMimeMessage();
        MimeMessageHelper mimeMessageHelper = new MimeMessageHelper(mimeMessage, true, "UTF-8");
        mimeMessageHelper.setFrom(senderName);
        mimeMessageHelper.setTo(toEmail.split(","));
        mimeMessageHelper.setSubject(subject);
        mimeMessageHelper.setText(text, true);

        /*if (!Strings.isNullOrEmpty(ccEmail)) {
            mimeMessageHelper.setCc(ccEmail.split(","));
        }
        if (!Strings.isNullOrEmpty(bccEmail)) {
            mimeMessageHelper.setBcc(bccEmail.split(","));
        }
        try {
            if (!CollectionUtils.isEmpty(files)) {
                for (File file : files) {
                    mimeMessageHelper.addAttachment(MimeUtility.encodeText(file.getName(), "GBK", "B"), file);
                }
            }
        } catch (Exception e) {
            System.out.println(e.getMessage());
        }*/
        return mimeMessage;
    }

    /*static String userName = "342883290@qq.com";
    static String password = "zzfxaakqvvpjbjjh";
    static String host = "smtp.qq.com";
    static String port = "465";
    static String ssl = "1";
    public static void main(String[] args) {
        String content = "<html><head></head><body>******</body></html>";
        sendMail(userName, password, host, ssl, Integer.valueOf(port), "liutangyun@egongcai.com", "测试",content);
    }*/
}
